/**
 * 
 */
package com.gopay.bis.web.controller.ebank;

import java.math.BigDecimal;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.gopay.bis.domain.order.OrderInfo;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.constants.bank.BisBankBusinessProd;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.cmb.ebank.pay.CmbB2cMessage;

/**
 * @CmbB2cFrontRes.java
 * @author Ryan
 * @2013-11-4 下午6:11:17 www.gopay.com.cn Inc.All rights reserved.
 */
@Controller
@Scope("prototype")
public class CmbB2cFrontRes extends BankResBase {

	private static Log logger = LogFactory.getLog(CmbB2cServerRes.class);

	@RequestMapping(value = "/cmbB2cFrontRes")
	protected ModelAndView execute(HttpServletRequest request,HttpServletResponse response) {
		logger.error("--------------- cmbB2cFrontRes bankback ---------------");
		return super.execute(request, response);
	}

	@Override
	protected BaseMessage buildMessage(HttpServletRequest request) {
		CmbB2cMessage message = new CmbB2cMessage();
		// 银行返回的信息
		message.setSignature(request.getQueryString());
		logger.error("signString : " + message);
		message.setBillNo(request.getParameter("BillNo"));
		message.setAmount(new BigDecimal(request.getParameter("Amount")));
		message.setBankOrderId(request.getParameter("BillNo"));
		message.setOrderId(request.getParameter("BillNo"));
		message.setSucceed(request.getParameter("Succeed"));
		message.setDate(request.getParameter("Date"));
		message.setMsg(request.getParameter("Msg"));
		message.setBank(BankConstants.BANK_LOGO_CMB);
        //银行业务产品00个人网银支付
        message.setBankBusProd(BisBankBusinessProd._00);
		return message;
	}

	@Override
	protected OrderInfo buildOrderInfo(HttpServletRequest request, Result result) {
		OrderInfo orderInfo = new OrderInfo();
		CmbB2cMessage message = null;
		if (result != null) {
			message = (CmbB2cMessage) result.getModel("message");
		}
		if (message != null) {
			orderInfo.setOrderId(message.getOrderId());
			orderInfo.setAmt(request.getParameter("Amount"));
			orderInfo.setNotifyNum("1");
			orderInfo.setPayStatus("");
			orderInfo.setRetCode("00");
			orderInfo.setRetMsg("");
			return orderInfo;
		} else {
			return null;
		}
	}

	@Override
	protected boolean checkParameters(HttpServletRequest request) {
		if (StringUtils.isEmpty(request.getQueryString())
				|| StringUtils.isEmpty(request.getParameter("BillNo"))
				|| StringUtils.isEmpty(request.getParameter("Amount"))
				|| StringUtils.isEmpty(request.getParameter("Succeed"))) {
			return false;
		}
		return true;
	}
}
